package com.mike.uaa.context;


import com.mike.uaa.core.Authentication;
import com.mike.uaa.core.GrantedAuthority;
import com.mike.uaa.core.User;
import org.springframework.util.function.SingletonSupplier;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author z zhang
 */
public class DebugLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {

    @Override
    public void clearContext() {

    }

    @Override
    public SecurityContext getContext() {
        return getDeferredContext().get();
    }

    @Override
    public DeferredSecurityContext getDeferredContext() {
        SecurityContext context = createEmptyContext();
        context.setAuthentication(createAuthenticatedToken());
        return new SupplierDeferredSecurityContext(SingletonSupplier.of(context), SecurityContextHolder.getContextHolderStrategy());
    }

    @Override
    public void setContext(SecurityContext context) {

    }

    @Override
    public SecurityContext createEmptyContext() {
        return new SecurityContextImpl();
    }

    private Authentication createAuthenticatedToken() {

        User user = new User();

        return new Authentication() {
            private static final long serialVersionUID = 7615063167518644592L;

            @Override
            public Object getPrincipal() {
                return user;
            }

            @Override
            public Object getCredential() {
                return null;
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return new ArrayList<>();
            }

            @Override
            public boolean isAuthenticated() {
                return false;
            }

            @Override
            public void setAuthenticated(boolean authenticated) {

            }
        };
    }
}
